package com.bjpowernode.future;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ClassName MyRunable
 *
 * @Author Cat
 */
public class MyRunnable implements Runnable {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }

    public static void main(String[] args) {
        //第一种方式：使用Thread去运行Runnable
        Thread t = new Thread(new MyRunnable());
        t.start();

        //lambda表达式实现一个Runnable
        Thread t2 = new Thread(() -> {
            System.out.println(Thread.currentThread().getName());
        });
        t2.start();

        //匿名内部类实现一个Runnable
        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
            }
        });
        t3.start();

        //第二种方式：使用“线程池”去运行Runnable
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.execute(new MyRunnable());
        executorService.submit(new MyRunnable());

        //把Runnable转成Callable
        Callable<Object> callable = Executors.callable(new MyRunnable());
        executorService.submit(callable);

        //关闭线程池
        executorService.shutdown();
    }
}